package io.github.biezhi.elves.spider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.github.biezhi.elves.qcc.model.CompAnalysis;
import io.github.biezhi.elves.qcc.model.CompBasicInfo;
import io.github.biezhi.elves.qcc.model.CompChangeRecord;
import io.github.biezhi.elves.qcc.model.CompQccCrawl;
import io.github.biezhi.elves.qcc.model.OutboundInvestment;
import io.github.biezhi.elves.service.QccRealTimeService;
import io.github.biezhi.elves.service.QccService;

@Component
public class QccRealTimeResolve {

	@Autowired
	private QccRealTimeService qccRealTimeService;
	
	//解析文档
	public void resolveQccHtml(){
		//查询t_comp_qcc_crawl表中的数据用于解析，取1000条
		List<CompQccCrawl> compQccCrawlList = qccRealTimeService.queryCompQccCrawl(100);
		
		for (CompQccCrawl compQccCrawl : compQccCrawlList) {
			System.out.println(compQccCrawl);
			CompAnalysis compAnalysis = new CompAnalysis();
			CompBasicInfo compBasicInfo = new CompBasicInfo();
//			CompChangeRecord compChangeRecord = new CompChangeRecord();
//			OutboundInvestment outboundInvestment = new OutboundInvestment();
			//企业id
			Long compId = compQccCrawl.getCompId();
			String pripid = compQccCrawl.getPripid();
			//企业名称
			String compName = compQccCrawl.getCompName();
			//企查查id
			String qccId = compQccCrawl.getQccId();
			
			//企业图谱json数据
			String companyBusinessmap = compQccCrawl.getCompanyBusinessmap();
//			companyBusinessmap = unicodeToUtf8(companyBusinessmap);
			companyBusinessmap = unicodeToString(companyBusinessmap);
			//投资族谱json数据
			String companyRelation = compQccCrawl.getCompanyRelation();
			companyRelation = unicodeToString(companyRelation);
			//关联图谱json数据
			String companyMuhou = compQccCrawl.getCompanyMuhou();
			companyMuhou = unicodeToString(companyMuhou);
			//股权结构图json数据
			String companyGuquan = compQccCrawl.getCompanyGuquan();
			companyGuquan = unicodeToString(companyGuquan);
			
			//插入到t_comp_basic_info表
			compBasicInfo.setCompId(compId);
			compBasicInfo.setPripid(pripid);
			compBasicInfo.setCompName(compName);
			compBasicInfo.setQccId(qccId);
			compBasicInfo.setCompAtlas(companyBusinessmap);
			compBasicInfo.setInvestmentGenealogy(companyRelation);
			compBasicInfo.setCorrelationMap(companyMuhou);
			compBasicInfo.setOwnershipStructureChart(companyGuquan);
			qccRealTimeService.insertCompBasicInfo(compBasicInfo);
			
			
			//企业详情html
			String compDetailHtml = compQccCrawl.getCompDetailHtml();
			Document doc = Jsoup.parse(compDetailHtml);
			String compLogo = doc.select("div.logo > div.imgkuang > img").attr("src");
//			System.out.println("企业logo:"+compLogo);
			
			String compPhone = "暂无";
			if (doc.select("span.fc > span.cvlu > span").text()!=null && 
					doc.select("span.fc > span.cvlu > span").text().length()>0) {
				compPhone = doc.select("span.fc > span.cvlu > span").text();
			}
//			System.out.println("企业电话:"+compPhone);
			
			String compSite = doc.select("div.row > span.cvlu.webauth > a").attr("href");
			if (compSite.equals("")) {
				compSite="暂无";
			}
//			System.out.println("企业官网:"+compSite);
			
			String compEmail="暂无";
			if (doc.select("div.row > span.fc > span.cvlu > a").attr("href") != null && 
					doc.select("div.row > span.fc > span.cvlu > a").attr("href").length()>0) {
				compEmail = doc.select("div.row > span.fc > span.cvlu > a").attr("href");
				if (compEmail.contains("mailto:")) {
					compEmail = compEmail.replace("mailto:", "");
				}
			}
//			System.out.println("企业邮箱:"+compEmail);
			
			String compAddress = "";
			if (doc.select("div.row > span.cvlu > a") != null && doc.select("div.row > span.cvlu > a").size()>0 && doc.select("div.row > span.cvlu > a").get(0) != null) {
				compAddress = doc.select("div.row > span.cvlu > a").get(0).text();
			}
//			System.out.println("企业地址:"+compAddress);
			
			String compDesc = "";
			if (doc.select("span.cdes").text().contains("简介")) {
				compDesc = doc.select("div.row > span.cvlu").text();
			}
//			System.out.println("企业简介:"+compDesc);	
			
			String legalInformation = doc.select("a.bname > h2").text();
//			System.out.println("法定代表人信息:"+legalInformation);
			
			String regcapText = doc.select("table.ntable").get(1).select("tbody tr").get(0).select("td").get(1).text();
//			System.out.println("注册资本:"+regcapText);
			
			String reccapText = doc.select("table.ntable").get(1).select("tbody tr").get(0).select("td").get(3).text();
//			System.out.println("实缴资本:"+reccapText);
			
			String status = doc.select("table.ntable").get(1).select("tbody tr").get(1).select("td").get(1).text();
//			System.out.println("经营状态:"+status);
			
			String estdate = doc.select("table.ntable").get(1).select("tbody tr").get(1).select("td").get(3).text();
//			System.out.println("成立日期:"+estdate);
			
			String unifiedSocialCreditCode= doc.select("table.ntable").get(1).select("tbody tr").get(2).select("td").get(1).text();
//			System.out.println("统一社会信用代码:"+unifiedSocialCreditCode);
			
			String taxpayerIdentNumber = doc.select("table.ntable").get(1).select("tbody tr").get(2).select("td").get(3).text();
//			System.out.println("纳税人识别号:"+taxpayerIdentNumber);
			
			String registrationNumber = doc.select("table.ntable").get(1).select("tbody tr").get(3).select("td").get(1).text();
//			System.out.println("注册号:"+registrationNumber);
			
			String organizationCode = doc.select("table.ntable").get(1).select("tbody tr").get(3).select("td").get(3).text();
//			System.out.println("组织机构代码:"+organizationCode);
			
			String companyType = doc.select("table.ntable").get(1).select("tbody tr").get(4).select("td").get(1).text();
//			System.out.println("公司类型:"+companyType);
			
			String industryName = doc.select("table.ntable").get(1).select("tbody tr").get(4).select("td").get(3).text();
//			System.out.println("所属行业:"+industryName);
			
			String apprdate = doc.select("table.ntable").get(1).select("tbody tr").get(5).select("td").get(1).text();
//			System.out.println("核准日期:"+apprdate);
			
			String regorg = doc.select("table.ntable").get(1).select("tbody tr").get(5).select("td").get(3).text();
//			System.out.println("登记机关:"+regorg);
			
			String country = doc.select("table.ntable").get(1).select("tbody tr").get(6).select("td").get(1).text();
//			System.out.println("所属地区:"+country);
			
			String countryEn = doc.select("table.ntable").get(1).select("tbody tr").get(6).select("td").get(3).text();
//			System.out.println("所属地区(英文名):"+countryEn);
			
			String nameUsed = doc.select("table.ntable").get(1).select("tbody tr").get(7).select("td").get(1).text();
//			System.out.println("曾用名:"+nameUsed);
			
			String personsNumber = doc.select("table.ntable").get(1).select("tbody tr").get(7).select("td").get(3).text();
//			System.out.println("参保人数:"+personsNumber);
			
			String personnelScale = doc.select("table.ntable").get(1).select("tbody tr").get(8).select("td").get(1).text();
//			System.out.println("人员规模:"+personnelScale);
			
			String businessTerm = doc.select("table.ntable").get(1).select("tbody tr").get(8).select("td").get(3).text();
//			System.out.println("营业期限:"+businessTerm);
			
			String opscope = doc.select("table.ntable").get(1).select("tbody tr").get(10).select("td").get(1).text();
//			System.out.println("经营范围:"+opscope);
			
			//插入到企业信息表t_comp_analysis
			compAnalysis.setCompId(compId);
			compAnalysis.setCompName(compName);
			compAnalysis.setQccId(qccId);
			compAnalysis.setCompLogo(compLogo);
			compAnalysis.setCompPhone(compPhone);
			compAnalysis.setCompSite(compSite);
			compAnalysis.setCompEmail(compEmail);
			compAnalysis.setCompAddress(compAddress);
			compAnalysis.setCompDesc(compDesc);
			compAnalysis.setLegalInformation(legalInformation);
			compAnalysis.setRegcapText(regcapText);
			compAnalysis.setReccapText(reccapText);
			compAnalysis.setStatus(status);
			compAnalysis.setEstdate(estdate);
			compAnalysis.setUnifiedSocialCreditCode(unifiedSocialCreditCode);
			compAnalysis.setTaxpayerIdentNumber(taxpayerIdentNumber);
			compAnalysis.setRegistrationNumber(registrationNumber);
			compAnalysis.setOrganizationCode(organizationCode);
			compAnalysis.setCompanyType(companyType);
			compAnalysis.setIndustryName(industryName);
			compAnalysis.setApprdate(apprdate);
			compAnalysis.setRegorg(regorg);
			compAnalysis.setCountry(country);
			compAnalysis.setCountryEn(countryEn);
			compAnalysis.setNameUsed(nameUsed);
			compAnalysis.setPersonsNumber(personsNumber);
			compAnalysis.setPersonnelScale(personnelScale);
			compAnalysis.setBusinessTerm(businessTerm);
			compAnalysis.setOpscope(opscope);
			
			qccRealTimeService.insertCompAnalysis(compAnalysis);
			
			//变更记录表
			if (doc.select("#Changelist")==null) {
				//说明该企业没变更记录信息
				CompChangeRecord compChangeRecord = new CompChangeRecord();
				compChangeRecord.setCompId(compId);
				compChangeRecord.setCompName(compName);
				compChangeRecord.setQccId(qccId);
				qccRealTimeService.insertCompChangeRecord(compChangeRecord);
			}else {
				Elements trs = doc.select("#Changelist > table.ntable > tbody > tr");
				for (int i = 1; i < trs.size(); i++) {
					CompChangeRecord compChangeRecord = new CompChangeRecord();
					Element tr = trs.get(i);
					//变更日期
					String dateChange = tr.select("td").get(1).text();
					//变更项目
					String prjChange = tr.select("td").get(2).text();
					//变更前
					String beforeChange = tr.select("td").get(3).text();
					//变更后
					String afterChange = tr.select("td").get(4).text();
					
					//插入到数据库t_comp_change_record
					compChangeRecord.setCompId(compId);
					compChangeRecord.setCompName(compName);
					compChangeRecord.setQccId(qccId);
					compChangeRecord.setDateChange(dateChange);
					compChangeRecord.setPrjChange(prjChange);
					compChangeRecord.setBeforeChange(beforeChange);
					compChangeRecord.setAfterChange(afterChange);
					qccRealTimeService.insertCompChangeRecord(compChangeRecord);
				}
			}
			
			//对外投资表
			if (doc.select("#touzilist")==null) {
				//说明该企业没变更记录信息
				OutboundInvestment outboundInvestment = new OutboundInvestment();
				outboundInvestment.setCompId(compId);
				outboundInvestment.setCompName(compName);
				outboundInvestment.setQccId(qccId);
				qccRealTimeService.insertOutboundInvestment(outboundInvestment);
			}else {
				Elements trs = doc.select("#touzilist > table.ntable.ntable-odd > tbody > tr");
				for (int i = 1; i < trs.size(); i++) {
					OutboundInvestment outboundInvestment = new OutboundInvestment();
					Element tr = trs.get(i);
					//被投资企业名称
					String investedCompName = tr.select("td").get(0).text();
					//被投资法定代表人
					String investedLegal = tr.select("td").get(1).text();
					//注册资本
					String regcapText2 = tr.select("td").get(2).text();
					//出资比例
					String investmentRate = tr.select("td").get(3).text();
					//成立日期
					String estdate2 = tr.select("td").get(4).text();
					//状态
					String status2 = tr.select("td").get(5).text();
					
					//插入到数据库t_comp_change_record
					outboundInvestment.setCompId(compId);
					outboundInvestment.setCompName(compName);
					outboundInvestment.setQccId(qccId);
					outboundInvestment.setInvestedCompName(investedCompName);
					outboundInvestment.setInvestedLegal(investedLegal);
					outboundInvestment.setRegcapText(regcapText2);
					outboundInvestment.setInvestmentRate(investmentRate);
					outboundInvestment.setEstdate(estdate2);
					outboundInvestment.setStatus(status2);
					
					//判断是否有多页
					if (doc.select("#touzilist > div > nav > ul.pagination") != null) {
						outboundInvestment.setOutboundInvestmentPage(1);
					}else{
						outboundInvestment.setOutboundInvestmentPage(0);
					}
					
					qccRealTimeService.insertOutboundInvestment(outboundInvestment);
				}
			}
			//将表状态改为已解析
			qccRealTimeService.updateResolveState(compId);
		}
		
	}
	
	public static void main(String[] args) {
		String companyBusinessmap = "{'Result':{'Node':{'name':'\u5e7f\u5dde\u5e02\u5929\u6cb3\u9ad8\u8fbe\u8f6e\u80ce\u8d38\u6613\u90e8','KeyNo':'173102549a337f864a5576ee509a3f2c','Category':1,'ShortName':'\u5929\u6cb3\u9ad8\u8fbe\u8f6e\u80ce','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':[{'name':'\u5bf9\u5916\u6295\u8d44','KeyNo':null,'Category':2,'ShortName':'\u5bf9\u5916\u6295\u8d44','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':null},{'name':'\u80a1\u4e1c','KeyNo':null,'Category':3,'ShortName':'\u80a1\u4e1c','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':null},{'name':'\u9ad8\u7ba1','KeyNo':null,'Category':4,'ShortName':'\u9ad8\u7ba1','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':[]},{'name':'\u5386\u53f2\u80a1\u4e1c','KeyNo':null,'Category':8,'ShortName':'\u5386\u53f2\u80a1\u4e1c','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':null},{'name':'\u5386\u53f2\u6cd5\u4eba','KeyNo':null,'Category':9,'ShortName':'\u5386\u53f2\u6cd5\u4eba','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':null},{'name':'\u88c1\u5224\u6587\u4e66','KeyNo':null,'Category':6,'ShortName':'\u88c1\u5224\u6587\u4e66','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':[]},{'name':'\u6cd5\u9662\u516c\u544a','KeyNo':null,'Category':5,'ShortName':'\u6cd5\u9662\u516c\u544a','Count':0,'Level':0,'FundedAmount':null,'FundedRate':null,'children':[]}]}},'Status':200,'Message':null}";
		String string = unicodeToUtf8(companyBusinessmap);
		System.out.println(string);
		QccRealTimeResolve qccResolve = new QccRealTimeResolve();
		String xmlString = qccResolve.getXmlString();
		Document doc = Jsoup.parse(xmlString);
//		企业logo
		String compLogo = doc.select("div.logo > div.imgkuang > img").attr("src");
		System.out.println("企业logo:"+compLogo);
//		企业电话
		String compPhone = "暂无";
		if (doc.select("span.fc > span.cvlu > span").text()!=null && 
				doc.select("span.fc > span.cvlu > span").text().length()>0) {
			compPhone = doc.select("span.fc > span.cvlu > span").text();
		}
		System.out.println("企业电话:"+compPhone);
//		企业官网
		String compSite = doc.select("div.row > span.cvlu.webauth > a").attr("href");
		if (compSite.equals("")) {
			compSite="暂无";
		}
		System.out.println("企业官网:"+compSite);
//		企业邮箱
		String compEmail="暂无";
		if (doc.select("div.row > span.fc > span.cvlu > a").attr("href") != null && 
				doc.select("div.row > span.fc > span.cvlu > a").attr("href").length()>0) {
			compEmail = doc.select("div.row > span.fc > span.cvlu > a").attr("href");
			if (compEmail.contains("mailto:")) {
				compEmail = compEmail.replace("mailto:", "");
			}
		}
		System.out.println("企业邮箱:"+compEmail);
//		企业地址
		String compAddress = "";
		if (doc.select("div.row > span.cvlu > a") != null && doc.select("div.row > span.cvlu > a").get(0) != null) {
			compAddress = doc.select("div.row > span.cvlu > a").get(0).text();
		}
		System.out.println("企业地址:"+compAddress);
//		企业简介
		String compDesc = "";
		if (doc.select("span.cdes").text().contains("简介")) {
			compDesc = doc.select("div.row > span.cvlu").text();
		}
		System.out.println("企业简介:"+compDesc);		
//		法定代表人信息
		String legalInformation = doc.select("a.bname > h2").text();
		System.out.println("法定代表人信息:"+legalInformation);
//		注册资本(带单位，单位是不同币种，样例数据：25000万美元)
		String regcapText = doc.select("table.ntable").get(1).select("tbody tr").get(0).select("td").get(1).text();
		System.out.println("注册资本:"+regcapText);
		//实缴资本(带单位，单位是不同币种，样例数据：200万美元)
		String reccapText = doc.select("table.ntable").get(1).select("tbody tr").get(0).select("td").get(3).text();
		System.out.println("实缴资本:"+reccapText);
		//经营状态
		String status = doc.select("table.ntable").get(1).select("tbody tr").get(1).select("td").get(1).text();
		System.out.println("经营状态:"+status);
//		成立日期
		String estdate = doc.select("table.ntable").get(1).select("tbody tr").get(1).select("td").get(3).text();
		System.out.println("成立日期:"+estdate);
		//统一社会信用代码
		String unifiedSocialCreditCode= doc.select("table.ntable").get(1).select("tbody tr").get(2).select("td").get(1).text();
		System.out.println("统一社会信用代码:"+unifiedSocialCreditCode);
//		纳税人识别号
		String taxpayerIdentNumber = doc.select("table.ntable").get(1).select("tbody tr").get(2).select("td").get(3).text();
		System.out.println("纳税人识别号:"+taxpayerIdentNumber);
		//注册号
		String registrationNumber = doc.select("table.ntable").get(1).select("tbody tr").get(3).select("td").get(1).text();
		System.out.println("注册号:"+registrationNumber);
		//组织机构代码
		String organizationCode = doc.select("table.ntable").get(1).select("tbody tr").get(3).select("td").get(3).text();
		System.out.println("组织机构代码:"+organizationCode);
		//公司类型
		String companyType = doc.select("table.ntable").get(1).select("tbody tr").get(4).select("td").get(1).text();
		System.out.println("公司类型:"+companyType);
		//所属行业
		String industryName = doc.select("table.ntable").get(1).select("tbody tr").get(4).select("td").get(3).text();
		System.out.println("所属行业:"+industryName);
		//核准日期
		String apprdate = doc.select("table.ntable").get(1).select("tbody tr").get(5).select("td").get(1).text();
		System.out.println("核准日期:"+apprdate);
		//登记机关
		String regorg = doc.select("table.ntable").get(1).select("tbody tr").get(5).select("td").get(3).text();
		System.out.println("登记机关:"+regorg);
		//所属地区
		String country = doc.select("table.ntable").get(1).select("tbody tr").get(6).select("td").get(1).text();
		System.out.println("所属地区:"+country);
		//所属地区(英文名)
		String countryEn = doc.select("table.ntable").get(1).select("tbody tr").get(6).select("td").get(3).text();
		System.out.println("所属地区(英文名):"+countryEn);
		//曾用名
		String nameUsed = doc.select("table.ntable").get(1).select("tbody tr").get(7).select("td").get(1).text();
		System.out.println("曾用名:"+nameUsed);
		//参保人数
		String personsNumber = doc.select("table.ntable").get(1).select("tbody tr").get(7).select("td").get(3).text();
		System.out.println("参保人数:"+personsNumber);
		//人员规模
		String personnelScale = doc.select("table.ntable").get(1).select("tbody tr").get(8).select("td").get(1).text();
		System.out.println("人员规模:"+personnelScale);
		//营业期限
		String businessTerm = doc.select("table.ntable").get(1).select("tbody tr").get(8).select("td").get(3).text();
		System.out.println("营业期限:"+businessTerm);
		//经营范围
		String opscope = doc.select("table.ntable").get(1).select("tbody tr").get(10).select("td").get(1).text();
		System.out.println("经营范围:"+opscope);
		

		
		ListIterator<Element> listIterator = doc.select("table.ntable").get(6).select("tbody tr").listIterator();
		while (listIterator.hasNext()) {
			Element element = (Element) listIterator.next();
			if (!(element.text().contains("变更前") && element.text().contains("变更后"))) {
				System.out.println(element.text());
			}
		}
	}
	
	public static String unicodeToUtf8(String unicodeStr){
		try {
			byte[] utf8  = unicodeStr.getBytes("UTF-8");
			String utf8Str = new String(utf8,"UTF-8");
			return utf8Str;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
public static String unicodeToString(String str) {
 
	Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
	Matcher matcher = pattern.matcher(str);
	char ch;
	while (matcher.find()) {
		//group 6728
		String group = matcher.group(2);
		//ch:'木' 26408
		ch = (char) Integer.parseInt(group, 16);
		//group1 \u6728
		String group1 = matcher.group(1);
		str = str.replace(group1, ch + "");
	}
	return str;
}

	
	//读取文本为字符串
	public String getXmlString() {
		String xmlString;
		char[] strBuffer = null;
		int flen = 0;
		File xmlfile = new File("D:\\qccHtml.html");
		try {
		FileInputStream input = new FileInputStream(xmlfile);
		InputStreamReader in=new InputStreamReader(input,"GBK");//字节流字符流转化的桥梁
		flen = (int)xmlfile.length();
		strBuffer = new char[flen];
		in.read(strBuffer, 0, flen);
		} catch (FileNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		xmlString = new String(strBuffer); //构建String时，可用char[]类型，

		return xmlString;
		}
	
}
